<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="static/easyui/themes/bootstrap/easyui.css">
<link rel="stylesheet" type="text/css" href="static/easyui/themes/icon.css">
<link rel="stylesheet" href="static/css/style.css" />
<link rel="stylesheet" href="static/css/tcommon.css" />
<script src="static/jquery/jquery-1.12.4.js"></script>
<script src="static/easyui/jquery.easyui.min.js"></script>
<script src="static/easyui/locale/easyui-lang-zh_CN.js"></script>
<title>工程量</title>
</head>

<body>
	<div class="content-container" data-options="region:'center'" title="工程量列表">
		<div class="panel-header">
			<div class="panel-title">工程量列表</div>
		</div>
		<table id="pro_table" class="easyui-datagrid" style="height: 100%">
		</table>

		<div id="pro_tb">
			<div class="search-input">
				项目：
				<input style="width: 200px;" id="projectitemid" name="projectitemid" class="easyui-combobox"
					editable="false"
					data-options="
							    valueField: 'id',
							    textField: 'name',
							    url: 'projectitemlist.do?status=2',
							    onLoadSuccess : function(data){ 
								   	if(data.length>0){
								   		$('#projectitemid').combobox('setValue',data[0].id);
							    	}
							    },
							    onSelect: function(rec){
						    		projectitemid = rec.id;
							    	if(rec.projectitemchilds.length>0){
										var url = 'projectitemchildlist?status=2&&projectitemid=' + rec.id;
										$('#projectitemchildid').combobox('reload', url);
										document.getElementById('div1').style.display = '';
							    	}else{
							    		projectitemchildid = 0;
										$('#projectitemchildid').combobox('clear');
										document.getElementById('div1').style.display = 'none';
							    		loadData();
							    	}
							    },panelHeight: 'auto'">
			</div>
			<div id="div1" style="display: none;" class="search-input">
				子项目：
				<input style="width: 200px;" id="projectitemchildid" name="projectitemchildid" class="easyui-combobox"
					editable="false"
					data-options="
							    valueField: 'id',
							    textField: 'name',
							    loadFilter: function (data) {
							        var obj = {};
							        obj.id = '';
							        obj.name = '全部';
							        data.splice(0, 0, obj);
							        return data;
							    },
							    onSelect: function(rec){
							    	projectitemchildid = rec.id;
						    		loadData();
							    },panelHeight: 'auto'">
			</div>
			<div class="search-input">
				状态：
				<select id="status" class="easyui-combobox" name="status" style="width: 120px;" editable="false"
					data-options="
							    onSelect: function(rec){
							   		status=rec.value;
							   		if(isnew){
							   			isnew = false;
							   		}else{
							   			
							   		}
							    },panelHeight: 'auto'">
					<option value="">全部</option>
					<option value="3">已拒绝</option>
					<option value="4">已审</option>
					<option value="6">待二级审核</option>
					<option value="7">待一级审核</option>
				</select>
			</div>
			<div class="search-input">
				发布日期：
				<input id="fromDate" class="easyui-datebox" editable="false" />
				~
				<input id="toDate" class="easyui-datebox" editable="false" />
				<a href="#" class="easyui-linkbutton" iconCls="icon-search" id="search">搜索</a>
			</div>
			<div>
				<a href="javascript:void(0)" class="d-btn btn-blue" onclick="newpro()">添加</a>
				<a href="javascript:void(0)" class="d-btn btn-green" onclick="updatepro()">编辑</a>
				<a href="javascript:void(0)" class="d-btn btn-red" onclick="deletepro()">删除</a>
			</div>
		</div>
	</div>
	<div id="proDlg" class="easyui-dialog info_dialog" style="width: 800px; padding: 10px 20px; top: 30px;"
		closed="true" buttons="#pro-dlg-buttons">
		<div class="title"></div>
		<form id="proFm" method="post">
			<div class="content_container">
				<table class="content_table">
					<tr>
						<td style="width: 100px;">项目：</td>
						<td>
							<input style="width: 200px;" id="projectitem" name="projectitem" class="easyui-combobox"
								editable="false"
								data-options="
							    valueField: 'id',
							    textField: 'name',
							   	required:true,
							    url: 'projectitemlist.do?status=2',
							    onSelect: function(rec){
								    if(rec.projectitemchilds.length>0){
										document.getElementById('tr1').style.display = '';
										document.getElementById('tr2').style.display = '';
										var url = 'projectitemchildlist?status=2&&projectitemid=' + rec.id;
										$('#projectitemchild').combobox('reload', url);
								    }else{
										document.getElementById('tr1').style.display = 'none';
										document.getElementById('tr2').style.display = 'none';
							    	}
							    },panelHeight: 'auto'">
						</td>
						<td id="tr1" style="display: none;">子项目</td>
						<td id="tr2" style="display: none;">
							<input style="width: 200px;" id="projectitemchild" name="projectitemchild" class="easyui-combobox"
								editable="false"
								data-options="
							    valueField: 'id',
							    textField: 'name',
							    onSelect: function(rec){
							    },panelHeight: 'auto'">
						</td>
					</tr>
					<tr>
						<td style="width: 100px;">名称及规格：</td>
						<td>
							<input class="easyui-textbox" data-options="required:true" id="name" name="name" style="width: 200px;" />
						</td>
					</tr>
					<tr>
						<td style="width: 100px;">单位：</td>
						<td>
							<input class="easyui-textbox" data-options="required:true" id="unit" name="unit" style="width: 200px;" />
						</td>
					</tr>
					<tr>
						<td style="width: 100px;">数量：</td>
						<td>
							<input class="easyui-textbox" data-options="required:true" id="quantity" name="quantity"
								style="width: 200px;" />
						</td>
					</tr>
					<tr>
						<td style="width: 100px;">备注：</td>
						<td colspan="3">
							<input class="easyui-textbox" data-options="required:true,multiline:true" id="remark" name="remark"
								style="width: 550px; height: 100px;" />
						</td>
					</tr>
				</table>
			</div>
		</form>
	</div>
	<div id="pro-dlg-buttons">
		<a href="# " class="easyui-linkbutton ok" onclick="savePro()">保存</a>
		<a href="# " class="easyui-linkbutton btn-cancel " onclick="javascript:$('#proDlg').dialog('close')">关闭</a>
	</div>
	<div style="display: none">
		<div id="displayAtt" class="datagrid-toolbar"
			style="width: 80%; max-height: 80%; padding: 5px; top: 30px; text-align: center;"></div>
	</div>
</body>
<script>
	var currentPro;
	var projectitemid = "";
	var projectitemchildid = "";
	var status = "";
	var isnew = true;
	var fromDate = "";
	var toDate = "";
	$("#search").click(function() {
		fromDate = $("#fromDate").datebox('getValue');
		toDate = $("#toDate").datebox('getValue');
		projectitemid = $("#projectitemid").combobox('getValue');
		projectitemchildid = $("#projectitemchildid").combobox('getValue');
		status = $("#status").combobox('getValue');
		loadData();
	});
	function loadData() {
		if (projectitemchildid > 0){
			$('#pro_table').datagrid({
				rownumbers : true,
				singleSelect : true,
				pagination : true,
				fitColumns : false,
				border : false,
				striped : true,
				toolbar : '#pro_tb',
				url : "quantitiess",
				method : 'get',
				queryParams : {
					startdate : fromDate,
					enddate : toDate,
					projectitemchildid : projectitemchildid,
					status : status
				},
				onSelect : function(index,row) {
					currentPro = row;
				},
				onDblClickRow : function(index,row) {
					currentPro = row;
					updatepro();
				},
				columns : [ [ {
					field : 'title',
					title : '项目名',
					width : '',
					formatter : function(value,row,index) {
						if (row.projectitemchild){
							return row.projectitemchild.name;
						}else{
							return row.item.name;
						}
					}
				},{
					field : 'name',
					title : '名称及规格',
					width : ''
				},{
					field : 'unit',
					title : '单位',
					width : ''
				},{
					field : 'quantity',
					title : '数量',
					width : ''
				},{
					field : 'remark',
					title : '备注',
					width : '200px'
				},{
					field : 'status',
					title : '状态',
					width : '100px',
					formatter : function(value,row,index) {
						if (value == 4){
							return value = '已审';
						}else if (value == 3){
							return value = '已拒绝';
						}else if (value == 6){
							return value = '待二级审核';
						}else if (value == 7){
							return value = '待一级审核';
						}else if (value == 5){
							return value = '完成';
						}
					}
				},{
					field : 'reason',
					title : '拒绝理由',
					width : '200px'
				} ] ]
			});
			if (status != 3){
				$('#pro_table').datagrid('hideColumn', 'reason');
			}
		}else{
			$('#pro_table').datagrid({
				rownumbers : true,
				singleSelect : true,
				pagination : true,
				fitColumns : false,
				border : false,
				striped : true,
				toolbar : '#pro_tb',
				url : "quantitiess",
				method : 'get',
				queryParams : {
					startdate : fromDate,
					enddate : toDate,
					projectitemid : projectitemid,
					status : status
				},
				onSelect : function(index,row) {
					currentPro = row;
				},
				onDblClickRow : function(index,row) {
					currentPro = row;
					updatepro();
				},
				columns : [ [ {
					field : 'title',
					title : '项目名',
					width : '',
					formatter : function(value,row,index) {
						if (row.projectitemchild){
							return row.projectitemchild.name;
						}else{
							return row.item.name;
						}
					}
				},{
					field : 'name',
					title : '名称及规格',
					width : ''
				},{
					field : 'unit',
					title : '单位',
					width : ''
				},{
					field : 'quantity',
					title : '数量',
					width : ''
				},{
					field : 'remark',
					title : '备注',
					width : '200px'
				},{
					field : 'status',
					title : '状态',
					width : '100px',
					formatter : function(value,row,index) {
						if (value == 4){
							return value = '已审';
						}else if (value == 3){
							return value = '已拒绝';
						}else if (value == 6){
							return value = '待二级审核';
						}else if (value == 7){
							return value = '待一级审核';
						}else if (value == 5){
							return value = '完成';
						}
					}
				},{
					field : 'reason',
					title : '拒绝理由',
					width : '200px'
				} ] ]
			});
		}
		if (status != 3){
			$('#pro_table').datagrid('hideColumn', 'reason');
		}
	}
	function newpro() {
		$("#proDlg").dialog('open').dialog('setTitle', '添加工程量');
		$("#proFm").form('clear');
		$("#projectitem").combobox("enable");
		$("#projectitemchild").combobox("enable");
		document.getElementById('tr1').style.display = 'none';
		document.getElementById('tr2').style.display = 'none';
		currentPro = {};
		$("#pro-dlg-buttons").find(".easyui-linkbutton.ok").show();
	}
	function updatepro() {
		currentPro = $("#pro_table").datagrid('getSelected');
		if (currentPro){
			$("#projectitem").combobox("setValue", currentPro.item.id);
			$("#projectitem").combobox("disable");
			if (currentPro.projectitemchild != null){
				$("#projectitemchild").combobox("setValue", currentPro.projectitemchild.id);
				$("#projectitemchild").combobox("disable");
			}
			$("#proFm").form('load', {
				name : currentPro.name,
				unit : currentPro.unit,
				quantity : currentPro.quantity,
				remark : currentPro.remark
			});
			if (currentPro.status != 6){
				$("#proDlg").dialog('open').dialog('setTitle', '工程量详细');
				$("#pro-dlg-buttons").find(".easyui-linkbutton.ok").hide();
			}else{
				$("#proDlg").dialog('open').dialog('setTitle', '更新工程量');
				$("#pro-dlg-buttons").find(".easyui-linkbutton.ok").show();
			}
		}else{
			$.messager.alert('更新', '请选择要更新的工程量', 'warning');
		}
	}
	function deletepro() {
		currentPro = $("#pro_table").datagrid('getSelected');
		if (currentPro){
			if (currentPro.status == 6){
				$.messager.confirm("操作提示", "您确定要执行操作吗？", function(data) {
					if (data){
						$.ajax({
							type : 'DELETE',
							url : "QuantitiesDelete.do?id=" + currentPro.id,
							contentType : "application/json",
							success : function(data) {
								if (data.success){
									$.messager.show({
										title : '删除',
										msg : '删除成功',
										timeout : 5000,
										showType : 'slide'
									});
									loadData();
								}else{
									$.messager.alert('删除', '删除失败', 'error');
								}
							},
							dataType : "json"
						});
					}
				});
			}else{
				$.messager.alert('警告', '已审批工程量不允许删除', 'warning');
			}
		}else{
			$.messager.alert('警告', '请选择要删除的工程量', 'warning');
		}
	}
	function savePro() {
		if (!$("#proFm").form('validate'))
			return;
		currentPro.projectitemid = $("#projectitem").combobox('getValue');
		var projectchildid = $("#projectitemchild").combobox('getValue');
		if (projectchildid != null){
			currentPro.projectitemchildid = projectchildid;
		}
		currentPro.name = $("#name").textbox('getValue');
		currentPro.unit = $("#unit").textbox('getValue');
		currentPro.quantity = $("#quantity").textbox('getValue');
		currentPro.remark = $("#remark").textbox('getValue');
		currentPro.status = 6;
		$.ajax({
			type : 'POST',
			url : "QuantitiesSave.do",
			data : JSON.stringify(currentPro),
			contentType : "application/json",
			success : function(data) {
				if (data.success){
					$.messager.show({
						title : '保存工程量',
						msg : '保存工程量成功',
						timeout : 5000,
						showType : 'slide'
					});
					loadData();
				}else{
					$.messager.alert('保存工程量', '保存工程量失败', 'error');
				}
				$('#proDlg').dialog('close');
			},
			dataType : "json"
		});
	}
	function getLastMonth() {
		var now = new Date();
		var year = now.getFullYear();
		var month = now.getMonth() + 1;//0-11表示1-12月
		var day = now.getDate() + 1;
		var dateObj = {};
		dateObj.now = year + '-' + month + '-' + day;
		var nowMonthDay = new Date(year, month, 0).getDate(); //当前月的总天数
		if (month - 1 <= 0){ //如果是1月，年数往前推一年<br>　　　　 
			dateObj.last = (year - 1) + '-' + 12 + '-' + day;
		}else{
			var lastMonthDay = new Date(year, (parseInt(month) - 1), 0).getDate();
			if (lastMonthDay < day){ //1个月前所在月的总天数小于现在的天日期
				if (day < nowMonthDay){ //当前天日期小于当前月总天数
					dateObj.last = year + '-' + (month - 1) + '-' + (lastMonthDay - (nowMonthDay - day));
				}else{
					dateObj.last = year + '-' + (month - 1) + '-' + lastMonthDay;
				}
			}else{
				dateObj.last = year + '-' + (month - 1) + '-' + day;
			}
		}
		return dateObj;
	}
</script>

</html>